package com.xie.service;

import com.xie.mapper.BaiduWapMapper;
import com.xie.pojo.BaiduWap;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Component
@Configuration
@EnableScheduling
public class ScheduleTask {

    @Autowired
    private BaiduWapMapper baiduWapMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 每分钟更新redis缓存数据
     */
    @Scheduled(cron = "* */1 * * * ?")
    @CacheEvict(cacheNames = "phb")
    public void test1(){
        List<BaiduWap> byDesc = baiduWapMapper.findByDesc();
        for (int i = 0; i < 10; i++) {
            redisTemplate.opsForList().rightPush("phb-name",byDesc.get(i).getName());
            redisTemplate.opsForList().rightPush("phb-count",byDesc.get(i).getCount().toString());
        }
    }

    /**
     * 每小时检测失效资源,检测不清理
     */
    @Scheduled(cron = "* * 0/1 * * ?")
    public void checkStatus() throws IOException {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        List<BaiduWap> baiduWaps = baiduWapMapper.selectAll();
        for (BaiduWap baiduWap : baiduWaps) {
            String url = baiduWap.getWww();
            HttpGet httpGet=new HttpGet("https://"+url);
            CloseableHttpResponse execute = httpClient.execute(httpGet);
            if (execute.getStatusLine().getStatusCode()==200){
                String rs = EntityUtils.toString(execute.getEntity(),"utf8");
                Document document = Jsoup.parseBodyFragment(rs);
                Element share_nofound_des = document.getElementById("share_nofound_des");
                if (share_nofound_des!=null){
//                    System.out.println("失效资源:"+baiduWap.getName());
//                    System.out.println("rs = " + rs);
                    baiduWap.setStatus(0);
                    baiduWapMapper.updateByPrimaryKey(baiduWap);
                }else {
//                    System.out.println(baiduWap.getName()+"[OK]");
                    if (baiduWap.getStatus()==0){
                        baiduWap.setStatus(1);
                        baiduWapMapper.updateByPrimaryKey(baiduWap);
                    }
                }
            }
        }
    }




}
